package com.masterclient.app.modules.goods.adapter;

import android.graphics.Typeface;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.masterclient.app.App;
import com.masterclient.app.R;
import com.eoner.baselibrary.bean.goods.ShopBean;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.Utils;
import com.eoner.baselibrary.utils.ViewTransformUtil;

import jp.wasabeef.glide.transformations.CropCircleWithBorderTransformation;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * 作者：sean
 * 创建时间：2017/6/23 16:32
 * 功能说明：
 */

public class ShopGoodRecyclerAdapter extends BaseQuickAdapter<ShopBean.DataBean.ShProductsBean, BaseViewHolder> {
//    @BindView(R.id.iv_goods)
//    ImageView ivGoods;
//    @BindView(R.id.tv_new_goods_promotion)
//    TextView tvNewGoodsPromotion;
//    @BindView(R.id.tv_sub_title)
//    TextView tvSubTitle;
//    @BindView(R.id.tv_title)
//    TextView tvTitle;
//    @BindView(R.id.tv_price_now)
//    TextView tvPriceNow;
//    @BindView(R.id.tv_price_older)
//    TextView tvPriceOlder;

    public ShopGoodRecyclerAdapter() {
        super(R.layout.item_search_good);
    }

    @Override
    protected void convert(BaseViewHolder helper, ShopBean.DataBean.ShProductsBean item) {

        ImageView ivGoods = (ImageView) helper.getView(R.id.iv_goods);
        ViewTransformUtil.glideImageView(mContext,item.getSh_image(),ivGoods,
                new RoundedCornersTransformation(ScreenUtils.dp2px(7), 0, RoundedCornersTransformation.CornerType.TOP),
                R.mipmap.defaults,ScreenUtils.dp2px(167), ScreenUtils.dp2px(167));

        TextView buy_giveaway = helper.getView(R.id.buy_giveaway);
        buy_giveaway.setVisibility(!TextUtils.isEmpty(item.getSh_tag())?View.VISIBLE:View.GONE);
        buy_giveaway.setText(TextUtils.isEmpty(item.getSh_tag())?"":item.getSh_tag());

        helper.setGone(R.id.overseas_direct_mail_ll,!TextUtils.isEmpty(item.getSh_country()));
        helper.setGone(R.id.overseas_direct_mail_tag,!TextUtils.isEmpty(item.getSh_send_type())&&"2".equals(item.getSh_send_type()));
        helper.setText(R.id.overseas_direct_mail_name,!TextUtils.isEmpty(item.getSh_country())?item.getSh_country():"");
        ViewTransformUtil.glideImageView(mContext,item.getSh_country_logo(),helper.getView(R.id.overseas_direct_mail_icon),
                new CropCircleWithBorderTransformation(0,mContext.getResources().getColor(R.color.white)),R.mipmap.defaults);
        ((TextView)helper.getView(R.id.overseas_direct_mail_name)).setMaxEms(!TextUtils.isEmpty(item.getSh_send_type())&&"2".equals(item.getSh_send_type())?4:20);


        helper.setText(R.id.tv_sub_title,item.getSh_name());
        helper.setText(R.id.tv_title,item.getSh_subname());
        helper.setText(R.id.tv_price_now,item.getSh_show_price());
        Utils.setDin(helper.getView(R.id.tv_price_now),mContext);

        TextView tv_bofang = helper.getView(R.id.tv_bofang);
        tv_bofang.setTypeface(Typeface.createFromAsset(mContext.getAssets(), "fonts/iconfont.ttf"));
//        if("1".equals(item.getSh_has_video())){
//            helper.getView(R.id.rl_video).setVisibility(View.VISIBLE);
//        }else {
//            helper.getView(R.id.rl_video).setVisibility(View.GONE);
//        }
//
//        if (TextUtils.isEmpty(item.getSh_line_price())||Double.valueOf(item.getSh_line_price()).doubleValue() == Double.valueOf(item.getSh_show_price()).doubleValue()) {
//            helper.getView(R.id.tv_price_older).setVisibility(View.INVISIBLE);
//        }else {
//            helper.getView(R.id.tv_price_older).setVisibility(View.VISIBLE);
//            helper.setText(R.id.tv_price_older,"¥"+item.getSh_line_price());
//        }

//        ((TextView)helper.getView(R.id.tv_price_older)).setPaintFlags(Paint. STRIKE_THRU_TEXT_FLAG| Paint.ANTI_ALIAS_FLAG);
//        helper.addOnClickListener(R.id.rl_goods);
        ImageView ivActivity = helper.getView(R.id.iv_activity);


        LinearLayout ll_user = helper.getView(R.id.ll_user);
        LinearLayout ll_earn = helper.getView(R.id.ll_earn);
        TextView tv_price = helper.getView(R.id.tv_price);
        TextView tv_money_earn = helper.getView(R.id.tv_money_earn);

        Utils.setDin(tv_money_earn,mContext);
        if(TextUtils.isEmpty(item.getSh_partner_commission())||0==Double.valueOf(item.getSh_partner_commission())){
            tv_money_earn.setVisibility(View.GONE);
        }else {
            tv_money_earn.setVisibility(View.VISIBLE);
            tv_money_earn.setText("赚"+item.getSh_partner_commission());
        }
//        if(0==Double.valueOf(item.getSh_partner_commission())){
//            ll_user.setVisibility(View.VISIBLE);
//            ll_earn.setVisibility(View.GONE);
//        }else {
//            tv_price.setText("¥"+item.getSh_show_price());
//            tv_money_earn.setText("赚"+item.getSh_partner_commission());
//            ll_user.setVisibility(View.GONE);
//            ll_earn.setVisibility(View.VISIBLE);
//        }
//
        ImageView ivTagNew = helper.getView(R.id.iv_tag_new);
        View ivShouqing = helper.getView(R.id.ll_shouqing);
//
//
        if(Long.valueOf(item.getSh_stock())<=0){
            ivShouqing.setVisibility(View.VISIBLE);
        }else {
            ivShouqing.setVisibility(View.GONE);
        }

        if("1".equals(item.getSh_is_new())){
            ivTagNew.setVisibility(View.VISIBLE);
        }else {
            ivTagNew.setVisibility(View.GONE);
        }

        TextView newcomer = helper.getView(R.id.newcomer_price);
        if (!TextUtils.isEmpty(item.getSh_activity_flag())&&"2".equals(item.getSh_activity_flag())){
            newcomer.setVisibility(View.VISIBLE);
            newcomer.setText(item.getSh_label_name());
            helper.setText(R.id.tv_price_now,item.getSh_new_user_promotion_price());
        }else {
            newcomer.setVisibility(View.GONE);
        }

        TextView tvActivityMj = helper.getView(R.id.tv_activity_mj);
        if (!TextUtils.isEmpty(item.getSh_meet_reduce_name())){
            tvActivityMj.setVisibility(View.VISIBLE);
            tvActivityMj.setText(item.getSh_meet_reduce_name());
        }else {
            tvActivityMj.setVisibility(View.GONE);
        }

        if(item.getSh_label()!=null&& !TextUtils.isEmpty(item.getSh_label().getSh_icon_height())&&!TextUtils.isEmpty(item.getSh_label().getSh_icon_width())) {
            App.picasso.load(item.getSh_label().getSh_icon()).fit().into(ivActivity);
            ivActivity.setVisibility(View.VISIBLE);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) ivActivity.getLayoutParams();
            params.height = ScreenUtils.dp2px(Integer.valueOf(item.getSh_label().getSh_icon_height()) / 2);
            params.width = ScreenUtils.dp2px(Integer.valueOf(item.getSh_label().getSh_icon_width()) / 2);
            switch (item.getSh_label().getSh_location()) {
                case "left_top":
                    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    break;
                case "right_top":
                    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                    break;
                case "left_bottom":
                    params.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.iv_goods);
                    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    params.setMargins(ScreenUtils.dp2px(7), 0, 0, ScreenUtils.dp2px(7));
                    break;
                case "right_bottom":
                    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                    params.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.iv_goods);
                    params.setMargins(0, 0, ScreenUtils.dp2px(7), ScreenUtils.dp2px(7));
                    break;
            }
            ivActivity.setLayoutParams(params);
        }else {
            ivActivity.setVisibility(View.GONE);
        }
    }
}
